import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui';                      // 引入element-ui
import 'element-ui/lib/theme-chalk/index.css';     
import { jwtDecode } from 'jwt-decode';

// Fontawesome
import { library } from '@fortawesome/fontawesome-svg-core'
import { faHome, faBook, faSortAmountDown, faThLarge, faSearch, faList, faArrowLeft, faArrowRight, faBookOpen, faStar, faCog } from '@fortawesome/free-solid-svg-icons'
import { faCheck } from '@fortawesome/free-solid-svg-icons'
import { faBookmark } from '@fortawesome/free-regular-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'

Vue.config.productionTip = false
import axios from 'axios'
import VueAxios from 'vue-axios'

axios.defaults.baseURL="http://localhost:8087"
// axios.defaults.baseURL="http://124.70.186.152:8087"

// 添加请求拦截器，在请求头中加authentication
axios.interceptors.request.use(
  config => {
    // 在发送请求之前检查JWT令牌是否过期
    const token = sessionStorage.getItem('authentication');
    console.log(token)
    if (token) {
      const decodedToken = jwtDecode (token);
      const currentTime = Date.now() / 1000; // 将时间转换为秒
      if (decodedToken.exp < currentTime) {
        // 如果令牌已过期，跳转到登录页面
        vue.$router.push('/login');
      } else {
        // 如果令牌未过期，将令牌添加到请求头中
        config.headers.authentication = token;
      }
      // config.headers.authentication = sessionStorage.getItem('authentication');
    }else{
      vue.$router.push('/login');
    }
 
    return config;
  },
  error => {
    return Promise.reject(error);
  }
  
);
// Vue.prototype.$axios = axios
Vue.use(VueAxios, axios)

Vue.use(ElementUI);

library.add(faHome, faBook, faThLarge, faSortAmountDown)
library.add(faSearch, faBookmark, faList, faArrowLeft)
library.add(faArrowRight, faBookOpen, faStar, faCog)
library.add(faCheck)
Vue.component('faicon', FontAwesomeIcon)

const vue= new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
